package com.example.rabbitmq.demo3_backup_exchange;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static com.example.rabbitmq.demo2_pub_confirm.ConfirmConfig.CONFIRM_EXCHANGE_NAME;

/**
 * @Author songyahui
 * @Date 2023/7/12 17:44
 * @Description
 */
@Configuration
public class BackupConfig {

    //--------------------------------------------备份交换机配置-------------------------------------------
    //交换机
    public static final String BACKUP_EXCHANGE_NAME = "backup_exchange";

    //队列
    public static final String BACKUP_QUEUE_NAME = "backup_queue";
    public static final String WARNING_QUEUE_NAME = "warning_queue";

//    关联备份交换机然后创建发布确认交换机
//    如果broker已经有confirmExchange要提前清除掉
    @Bean("confirmExchange")
    public DirectExchange confirmExchange(){
        return ExchangeBuilder
                .directExchange(CONFIRM_EXCHANGE_NAME)
                .durable(true)
                .alternate(BackupConfig.BACKUP_EXCHANGE_NAME)
                .build();
    }

    //声明交换机
    @Bean("backupExchange")
    public FanoutExchange backupExchange(){
        return new FanoutExchange(BACKUP_EXCHANGE_NAME);
    }

    //声明队列
    @Bean("backupQueue")
    public Queue backupQueue(){
        return QueueBuilder.durable(BACKUP_QUEUE_NAME).build();
    }

    @Bean("warningQueue")
    public Queue warningQueue(){
        return QueueBuilder.durable(WARNING_QUEUE_NAME).build();
    }

    @Bean
    public Binding queueBackupBindingExchange(@Qualifier("backupQueue") Queue backupQueue, @Qualifier("backupExchange") FanoutExchange exchange){
        return BindingBuilder.bind(backupQueue).to(exchange);
    }

    @Bean
    public Binding queueWarningBindingExchange(@Qualifier("warningQueue") Queue warningQueue, @Qualifier("backupExchange") FanoutExchange exchange){
        return BindingBuilder.bind(warningQueue).to(exchange);
    }
}
